package com.hengpeng.itfints.controller.activity;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.util.StringHelper;
import com.hengpeng.itfinbase.en.RewardRedPacketStatus;
import com.hengpeng.itfinbase.en.RewardType;
import com.hengpeng.itfinbase.persist.UserInfo;
import com.hengpeng.itfints.controller.BaseController;
import com.hengpeng.itfints.controller.activity.vo.RewardRedpacketVo;
import com.hengpeng.itfints.tools.PageUtil;
import com.hengpeng.itfints.vo.MemberSession;

@Controller
@RequestMapping(value = "/redpacket")
public class RewardRedpacketController extends BaseController {

    /**
     * 获取红包列表（根据红包状态）
     * 已弃用
     * @param request
     * @param map
     * @param status
     * @param curPage
     * @return
     */
    @RequestMapping(value = "list")
    public String getList(HttpServletRequest request, ModelMap map, RewardRedPacketStatus status, String curPage) {
        MemberSession session = getSessionMember(request);
        if (session == null) {
            return "/page/login/login.jsp";
        }
        UserInfo userInfo = session.getUser();
        int int_curPage = (!StringHelper.isEmpty(curPage) ? Integer.parseInt(curPage) : 1);
        ServiceResult<List<RewardRedpacketVo>> serviceResult = serviceFactory.getActivityService().findRewardRedpacket(
                userInfo.getId(), status, int_curPage, 1000);
        List<RewardRedpacketVo> canuseList = new ArrayList<RewardRedpacketVo>();
        List<RewardRedpacketVo> usedList = new ArrayList<RewardRedpacketVo>();
        List<RewardRedpacketVo> outdateList = new ArrayList<RewardRedpacketVo>();
        Long canuserMoney = 0L;
        Long usedMoney = 0L;
        Long outdateMoney = 0L;
        if (serviceResult.getReturnValue() != null) {
            for (RewardRedpacketVo rewardRedpacketVo : serviceResult.getReturnValue()) {
                if (rewardRedpacketVo.getStatus().equals(RewardRedPacketStatus.CANUSE)) {
                    canuseList.add(rewardRedpacketVo);
                    canuserMoney += rewardRedpacketVo.getMoney();
                }
                if (rewardRedpacketVo.getStatus().equals(RewardRedPacketStatus.USEED)) {
                    usedList.add(rewardRedpacketVo);
                    usedMoney += rewardRedpacketVo.getMoney();
                }
                if (rewardRedpacketVo.getStatus().equals(RewardRedPacketStatus.OUTDATE)) {
                    outdateList.add(rewardRedpacketVo);
                    outdateMoney += rewardRedpacketVo.getMoney();
                }
            }
        }
        PageUtil pageUtil = new PageUtil(10, serviceResult.getReturnListSize(), int_curPage);
        map.put("dataList", serviceResult.getReturnValue());
        map.put("canuseList", canuseList);
        map.put("usedList", usedList);
        map.put("outdateList", outdateList);
        map.put("canuserMoney", canuserMoney);
        map.put("usedMoney", usedMoney);
        map.put("outdateMoney", outdateMoney);
        map.put("PageUtil", pageUtil);
        map.put("status", status == null ? RewardRedPacketStatus.CANUSE : status);
        return "/page/account/reward/redpacket.jsp";
    }

    /**
     * 获取红包列表（根据红包类型）
     * 
     * @param request
     * @param map
     * @param status
     * @param curPage
     * @return
     */
    @RequestMapping(value = "redlist")
    public String getRegRedpacket(HttpServletRequest request, ModelMap map, Integer type, String curPage) {
        MemberSession session = getSessionMember(request);
        if (session == null) {
            return "/page/login/login.jsp";
        }
        UserInfo userInfo = session.getUser();
        int int_curPage = (!StringHelper.isEmpty(curPage) ? Integer.parseInt(curPage) : 1);
        
        ServiceResult<List<RewardRedpacketVo>> serviceResult = new ServiceResult<List<RewardRedpacketVo>>();
        ServiceResult<List<RewardRedpacketVo>> allResult = new ServiceResult<List<RewardRedpacketVo>>();
        if (null != type && type == 110) { // type 为110 表示 已使用红包
            serviceResult = serviceFactory.getActivityService().findRegRedpacketByStatus(userInfo.getId(), type,
                    RewardRedPacketStatus.USEED, int_curPage, 10);// 根据红包类型type查询红包
            allResult = serviceFactory.getActivityService().findRegRedpacketByStatus(userInfo.getId(), 0,
                    RewardRedPacketStatus.USEED, 1, Integer.MAX_VALUE);// 全部红包
        } else {
            serviceResult = serviceFactory.getActivityService().findRegRedpacketByStatus(userInfo.getId(), type,
                    RewardRedPacketStatus.CANUSE, int_curPage, 10);// 根据红包类型type查询红包
            allResult = serviceFactory.getActivityService().findRegRedpacketByStatus(userInfo.getId(), 0,
                    RewardRedPacketStatus.CANUSE, 1, Integer.MAX_VALUE);// 全部红包
        }
        
        Long money = 0L;
        Long regMoney = 0L;
        Long investMoney = 0L;
        Long shareMoney = 0L;
        if (serviceResult.getReturnValue() != null) {
            for (RewardRedpacketVo rewardRedpacketVo : serviceResult.getReturnValue()) {
                money += rewardRedpacketVo.getMoney();
            }
        }
        if (allResult.getReturnValue() != null) {
            for (RewardRedpacketVo rewardRedpacketVo : allResult.getReturnValue()) {
                if (rewardRedpacketVo.getRewardType().equals(RewardType.REGISTER_RED)
                        || rewardRedpacketVo.getRewardType().equals(RewardType.INVITE_ONE_RED)
                        || rewardRedpacketVo.getRewardType().equals(RewardType.INVITE_TWO_RED)
                        || rewardRedpacketVo.getRewardType().equals(RewardType.INVITE_THREE_RED)) {// 注册红包
                    regMoney += rewardRedpacketVo.getMoney();
                }
                if (rewardRedpacketVo.getRewardType().equals(RewardType.INVEST_RED)) {// 投资红包
                    investMoney += rewardRedpacketVo.getMoney();
                }
                if (rewardRedpacketVo.getRewardType().equals(RewardType.SHARE_ONE_RED)
                        || rewardRedpacketVo.getRewardType().equals(RewardType.SHARE_TWO_RED)
                        || rewardRedpacketVo.getRewardType().equals(RewardType.SHARE_THREE_RED)
                        || rewardRedpacketVo.getRewardType().equals(RewardType.SHARE_FOUR_RED)
                        || rewardRedpacketVo.getRewardType().equals(RewardType.SHARE_FIVE_RED)
                        || rewardRedpacketVo.getRewardType().equals(RewardType.SHARE_SIX_RED)) {
                    shareMoney += rewardRedpacketVo.getMoney();
                }
            }
        }
        PageUtil pageUtil = new PageUtil(10, serviceResult.getReturnListSize(), int_curPage);
        map.put("dataList", serviceResult.getReturnValue());
        map.put("regMoney", regMoney);
        map.put("investMoney", investMoney);
        map.put("shareMoney", shareMoney);
        map.put("PageUtil", pageUtil);
        map.put("type", type == null ? 1 : type);
        if (type ==2) {
            return "/page/account/reward/investredpacket.jsp";
        }
        return "/page/account/reward/redpacket.jsp";
    }
    
    /**
     * 获取可用红包 20151028 lugaofei
     */
    @RequestMapping(value="getRedpacket")
    public String getRedpacket(HttpServletRequest request,ModelMap map){
        MemberSession session = getSessionMember(request);
        UserInfo user = session.getUser();
        ServiceResult<List<RewardRedpacketVo>> serviceResult = serviceFactory.getActivityService().findRewardRedpacket(user.getId(), RewardRedPacketStatus.CANUSE, 1, 10000);
        map.put("dataList", serviceResult.getReturnValue());
        
        return "/page/borrowInfo/out.jsp";
    }
}
